//
//  CNIntroduceViewController.m
//  chuiniu
//
//  Created by zheng ji on 14-8-8.
//  Copyright (c) 2014年 jxw. All rights reserved.
//

#import "CNIntroduceViewController.h"
//#import "CNViewController.h"
#import "CNMainViewController.h"
@interface CNIntroduceViewController ()
{
    UIScrollView* _scrollView;
    UIPageControl* _pageControl;
}
@end

@implementation CNIntroduceViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    int imageWidth=230,imageHeight=340;
    int startY=30;
    float screenHeight=self.view.frame.size.height;
    
    if(screenHeight>480){
       startY=30;
        imageWidth=280,imageHeight=430;
    }else if(screenHeight <480){
        imageWidth=210,imageHeight=320;
    }
    
    CGRect viewFrame=self.view.frame;
    int scrollViewHeight=viewFrame.size.height -40;
    
    
    CGRect scrollViewFrame=CGRectMake(viewFrame.origin.x, viewFrame.origin.y, viewFrame.size.width, scrollViewHeight);
    
    self.view.backgroundColor=[UIColor blueColor];

    _scrollView = [[UIScrollView alloc]initWithFrame:scrollViewFrame];    //scrollView的大小
    _scrollView.backgroundColor=[UIColor whiteColor];
    _scrollView.pagingEnabled=YES;//以页为单位滑动，即自动到下一页的开始边界
    _scrollView.showsVerticalScrollIndicator=NO;
    _scrollView.showsHorizontalScrollIndicator=NO;//隐藏垂直和水平显示条
    _scrollView.delegate=self;
    
    _scrollView.contentSize=CGSizeMake(viewFrame.size.width*4, scrollViewHeight);
    _scrollView.backgroundColor=[UIColor clearColor];
    
    CGRect pageControlFrame=CGRectMake((viewFrame.size.width - 100)/2.0, (_scrollView.frame.origin.y+ _scrollView.frame.size.height +5), 100, 20);
    
    _pageControl=[[UIPageControl alloc]initWithFrame:pageControlFrame];
    _pageControl.numberOfPages=4;
    _pageControl.currentPage=0;
    
    [self.view addSubview:_pageControl];
    
    UIImageView* imageView1=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"introduce_1.png"]];
    imageView1.frame=CGRectMake((viewFrame.size.width - imageWidth)/2.0, startY, imageWidth, imageHeight);
    
    [_scrollView addSubview:imageView1];
    
   
    
    UILabel* lbl1=[[UILabel alloc]initWithFrame:CGRectMake((viewFrame.size.width - 250)/2.0+5, imageView1.frame.origin.y + imageView1.frame.size.height +5 , 250, 20)];
    [lbl1 setTextColor:[UIColor whiteColor]];
    lbl1.text=@"1.摇动手机或轻点己方骰蛊开局";
    lbl1.backgroundColor=[UIColor clearColor];
    [_scrollView addSubview:lbl1];
    
    
    UIImageView* imageView2=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"introduce_2.png"]];
    imageView2.frame=CGRectMake(viewFrame.size.width +(viewFrame.size.width - imageWidth)/2.0, startY, imageWidth, imageHeight);
    
    [_scrollView addSubview:imageView2];
    
    
    
    int lbl2X=viewFrame.size.width + (viewFrame.size.width - 250)/2.0+5;
    UILabel* lbl2=[[UILabel alloc]initWithFrame:CGRectMake(lbl2X, imageView2.frame.origin.y + imageView2.frame.size.height +5 , 250, 20)];
    [lbl2 setTextColor:[UIColor whiteColor]];
    lbl2.text=@"2.猜测的数量或点数须大于对方";
    lbl2.backgroundColor=[UIColor clearColor];
    [_scrollView addSubview:lbl2];
    
    
    UIImageView* imageView3=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"introduce_3.png"]];
    imageView3.frame=CGRectMake(2*viewFrame.size.width +(viewFrame.size.width - imageWidth)/2.0, startY, imageWidth, imageHeight);
    
    [_scrollView addSubview:imageView3];
    
    
    int lbl3X=viewFrame.size.width*2 + (viewFrame.size.width - 250)/2.0+5;
    UILabel* lbl3=[[UILabel alloc]initWithFrame:CGRectMake(lbl3X, imageView2.frame.origin.y + imageView3.frame.size.height +5 , 250, 20)];
    [lbl3 setTextColor:[UIColor whiteColor]];
    lbl3.text=@"3.轻点对方骰蛊查看胜负";
    lbl3.backgroundColor=[UIColor clearColor];
    [_scrollView addSubview:lbl3];
    
    
    
    
    UIImageView* imageView4=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"introduce_4.png"]];
    imageView4.frame=CGRectMake(3*viewFrame.size.width +(viewFrame.size.width - imageWidth)/2.0, startY, imageWidth, imageHeight);
    
    [_scrollView addSubview:imageView4];
    
    int lbl4X=viewFrame.size.width*3 + (viewFrame.size.width - 250)/2.0+5;
    UILabel* lbl4=[[UILabel alloc]initWithFrame:CGRectMake(lbl4X, imageView2.frame.origin.y + imageView4.frame.size.height +5 , 250, 20)];
    [lbl4 setTextColor:[UIColor whiteColor]];
    lbl4.text=@"4.点数1是万能的,但猜过就失效";
    lbl4.backgroundColor=[UIColor clearColor];
    [_scrollView addSubview:lbl4];
    
    UIView* viewStartGame=[[UIView alloc]initWithFrame:CGRectMake(viewFrame.size.width*3 +(viewFrame.size.width - 100)/2.0, lbl4.frame.origin.y +lbl4.frame.size.height, 130, 40)];
    viewStartGame.backgroundColor=[UIColor purpleColor];
    viewStartGame.layer.cornerRadius=5.5;
    viewStartGame.layer.borderColor=[UIColor purpleColor].CGColor;
    
    [_scrollView addSubview:viewStartGame];
    
   // UILabel* lblStartGame=[[UILabel alloc]initWithFrame:CGRectMake(viewFrame.size.width*3 +(viewFrame.size.width - 100)/2.0, lbl4.frame.origin.y +lbl4.frame.size.height, 130, 30)];
    
    UILabel* lblStartGame=[[UILabel alloc]initWithFrame:CGRectMake(5, 5, 120, 30)];
    
     lblStartGame.backgroundColor=[UIColor purpleColor];
    lblStartGame.layer.cornerRadius=10.5;
    lblStartGame.layer.borderColor=[UIColor whiteColor].CGColor;
    lblStartGame.textColor=[UIColor whiteColor];
    lblStartGame.textAlignment=NSTextAlignmentCenter;
     lblStartGame.text=@"点击我开始挑战";
    lblStartGame.userInteractionEnabled=YES;
    
    UITapGestureRecognizer* tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(startGame)];
    tapGesture.numberOfTapsRequired=1;
    tapGesture.numberOfTouchesRequired=1;
    
    [lblStartGame addGestureRecognizer:tapGesture];
  
    [viewStartGame addSubview:lblStartGame];
    //[_scrollView addSubview:lblStartGame];
    
    
    [self.view addSubview:_scrollView];
    
    
    // Do any additional setup after loading the view.
}
-(void)startGame{
    UIStoryboard* storyBoard=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    NSUserDefaults* userDefaults=[NSUserDefaults standardUserDefaults];
    
    [userDefaults setObject:@"1" forKey:@"FirstRun"];
    [userDefaults synchronize];
    CNMainViewController* viewCtrl=(CNMainViewController*)[storyBoard instantiateViewControllerWithIdentifier:@"CNMainViewController"];
    
    [self presentViewController:viewCtrl animated:YES completion:nil];

}
- (void)scrollViewDidScroll:(UIScrollView*)sender

{
    
    CGFloat pageWidth = _scrollView.frame.size.width;
    
    int page = floor((_scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    
    _pageControl.currentPage = page;
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
